package handler

import (
	"hrms-api/model"
	"hrms-api/service"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// AttendanceRequest 考勤请求结构
type AttendanceRequest struct {
	UserID    uint      `json:"user_id" binding:"required"`
	Date      time.Time `json:"date" binding:"required"`
	Location  string    `json:"location" binding:"max=200"`
	Remark    string    `json:"remark" binding:"max=500"`
	LeaveType int       `json:"leave_type" binding:"oneof=0 1 2 3 4"`
	LeaveTime float32   `json:"leave_time" binding:"min=0"`
}

// CheckIn 员工签到
func CheckIn(c *gin.Context) {
	var req AttendanceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		BadRequest(c, "无效的请求参数")
		return
	}

	attendance, err := service.NewAttendanceService(model.DB).CheckIn(req.UserID, req.Date, req.Location, req.Remark)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			BadRequest(c, "用户不存在")
		} else {
			InternalServerError(c, "签到失败")
		}
		return
	}

	Success(c, attendance)
}

// CheckOut 员工签退
func CheckOut(c *gin.Context) {
	var req AttendanceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		BadRequest(c, "无效的请求参数")
		return
	}

	attendance, err := service.NewAttendanceService(model.DB).CheckOut(req.UserID, req.Date, req.Location, req.Remark)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			BadRequest(c, "未找到考勤记录")
		} else {
			InternalServerError(c, "签退失败")
		}
		return
	}

	Success(c, attendance)
}

// ApplyLeave 申请请假
func ApplyLeave(c *gin.Context) {
	var req AttendanceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		BadRequest(c, "无效的请求参数")
		return
	}

	attendance, err := service.NewAttendanceService(model.DB).ApplyLeave(req.UserID, req.Date, req.LeaveType, req.LeaveTime, req.Remark)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			BadRequest(c, "用户不存在")
		} else {
			InternalServerError(c, "请假失败")
		}
		return
	}

	Success(c, attendance)
}

// GetAttendance 获取考勤记录
func GetAttendance(c *gin.Context) {
	attendanceID := c.Param("id")

	attendance, err := service.NewAttendanceService(model.DB).GetAttendance(attendanceID)
	if err != nil {
		NotFound(c, "未找到考勤记录")
		return
	}

	Success(c, attendance)
}

// ListAttendances 获取考勤记录列表
func ListAttendances(c *gin.Context) {
	userID := c.Query("user_id")
	startDate := c.Query("start_date")
	endDate := c.Query("end_date")

	attendances, err := service.NewAttendanceService(model.DB).ListAttendances(userID, startDate, endDate)
	if err != nil {
		InternalServerError(c, "未能取得考勤记录")
		return
	}

	Success(c, attendances)
}

// GetTodayAttendance 获取今日考勤
func GetTodayAttendance(c *gin.Context) {
	userID := c.GetUint("user_id")

	attendance, err := service.NewAttendanceService(model.DB).GetTodayAttendance(userID)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			Success(c, nil)
		} else {
			InternalServerError(c, "Failed to get today's attendance")
		}
		return
	}

	Success(c, attendance)
}

// GetAttendanceHistory 获取考勤历史
func GetAttendanceHistory(c *gin.Context) {
	userID := c.GetUint("user_id")
	startDate := c.Query("start_date")
	endDate := c.Query("end_date")

	attendances, err := service.NewAttendanceService(model.DB).GetAttendanceHistory(userID, startDate, endDate)
	if err != nil {
		InternalServerError(c, "Failed to get attendance records")
		return
	}

	Success(c, attendances)
}

// GetAttendanceRecords 获取考勤记录
func GetAttendanceRecords(c *gin.Context) {
	userID := c.GetUint("user_id")
	courseID := c.Query("course_id")

	attendances, err := service.NewAttendanceService(model.DB).GetAttendanceRecords(userID, courseID)
	if err != nil {
		InternalServerError(c, "Failed to get attendance records")
		return
	}

	Success(c, attendances)
}
